package utils;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;

import session_beans.AirTransportSessionRemote;


public final class ATSessionCreator {

	
	public static final AirTransportSessionRemote createSessionBean(ServletContext servletContext) {
		
		AirTransportSessionRemote airTransportSession;
		
		airTransportSession = (AirTransportSessionRemote) servletContext.getAttribute("airTransportSession");
		
		if ( airTransportSession == null ) {
			Properties properties = new Properties();
			properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
			properties.put("java.naming.factory.url.pkgs","=org.jboss.naming:org.jnp.interfaces");
			properties.put("java.naming.provider.url","localhost:1099");
			
			Context context;
			try {
				context = new InitialContext(properties);
				
				String remoteName = "AirTransport_EAR/AirTransportSessionBean/remote";
				
				airTransportSession = (AirTransportSessionRemote)context.lookup(remoteName);
			}
			catch (NamingException ne) {
				ne.printStackTrace();
			}
			servletContext.setAttribute("airTransportSession", airTransportSession);
		}
		
		return airTransportSession;
	}
	
}
